package com.ddstation.prescription_violate.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddstation.common.model.InvsCheckCommonSchParModel;
import com.ddstation.crm.outpatient.model.OutpatientModel;
import com.ddstation.crm.outpatient.service.OutpatientService;
import com.ddstation.crud.entity.DtInvsViolationManageEntity;
import com.ddstation.crud.mapper.DtInvsViolationManageMapper;
import com.ddstation.home_page.model.HpCommonParModel;
import com.ddstation.prescription_violate.mapper.PrescriptionViolateMapper;
import com.ddstation.prescription_violate.model.InstitutionViolateResModel;
import com.ddstation.prescription_violate.model.RealTimeMonitorModel;
import com.ddstation.prescription_violate.model.ViolateDetailParaModel;
import com.ddstation.prescription_violate.model.ViolateDetailResModel;
import com.ddstation.statement.model.IllegalTypeModel;

@Service
public class PrescriptionViolateServiceImpl implements PrescriptionViolateService {
	
	// 实时监控记录
	@Override
	@Transactional(readOnly=true)
	public	List<RealTimeMonitorModel> searchRealTimeMonitor(HpCommonParModel _p){
		return prescriptionViolateMapper.selectRealTimeMonitor(_p);
	}
	
	// 机构别违规数据汇总
	@Override
	@Transactional(readOnly=true)
	public List<InstitutionViolateResModel> searchViolateCntGroupbyInstitutionId(InvsCheckCommonSchParModel _p){
		// 根据机构别汇总
		List<InstitutionViolateResModel> listOut = prescriptionViolateMapper.selectViolateCntGroupbyInstitutionId(_p);
		return listOut;
//		if ( listOut == null || listOut.size()==0 ){
//			return listOut;
//		}
//		// 查出明细数据，计算 已跟进、未跟进
//		List<RealTimeMonitorModel> listDetail = searchRealTimeMonitor(_p);//明细数据
//		for (InstitutionViolateResModel one:listOut){
//			// 根据明细过滤出未跟进的
//			List<RealTimeMonitorModel> fillerList = listDetail.stream().filter( 
//					a -> a.getFollowStatus() == 0 && a.getInstitutionId().equals(one.getInstitutionId())
//					).collect(Collectors.toList());
//			if ( fillerList != null ){
//				one.setNoFollowUp(fillerList.size());
//			} else {
//				one.setNoFollowUp(0);
//			}
//			// 计算出已跟进的
//			one.setHaveFollowUp(one.getViolateCnt() - one.getNoFollowUp());
//		}
		
	}
	
	// 指定机构处方违规明细查询
	@Override
	@Transactional(readOnly=true)
	public List<ViolateDetailResModel> searchDataList(ViolateDetailParaModel _p){
		return prescriptionViolateMapper.selectDataList(_p);
	}
	
	// 跟进信息更新
	@Override
	@Transactional(rollbackFor=Exception.class)
	public int updateFollowStatus(DtInvsViolationManageEntity w){
		return  prescriptionViolateMapper.updateFollowStatus(w);
	}
	
	/**
	 * 根据处方违规数据查询原处方信息
	 */
	@Override
	@Transactional(readOnly=true)
	public OutpatientModel serachOutpatient(String dviolationManageId){
		DtInvsViolationManageEntity vio = dtInvsViolationManageMapper.selectByPrimaryKey(dviolationManageId);
		
//		if ( vio == null || vio.getMedicalId() == null || vio.getPrescriptionType() == null){
//			throw new RuntimeException("未查询到处方违规数据，或者数据中处方类型为空，表：dt_invs_violation_manage，id:"+dviolationManageId);
//		}
//		
//		if ( vio.getPrescriptionType().intValue() != 20 ){
//			throw new RuntimeException("目前只支持查看西药处方。id:" +dviolationManageId + ",prescription_type"+vio.getPrescriptionType());
//		}
		return outpatientService.load(vio.getUid(), vio.getChildId(),vio.getMedicalId(), 2);
	}
	
	// 查询处方违规分类
	@Override
	@Transactional(readOnly=true)
	public List<IllegalTypeModel> searchIllegalTypePrescription(HpCommonParModel _p){
		return prescriptionViolateMapper.selectIllegalTypePrescription(_p);
	} 
	
	
	/**
     * 查询导出用数据
     * */
	@Override
	@Transactional(readOnly=true)
    public List<String[]>  getExcelData(
    		String invsInstitutionId,
    		String fromDt,
			String toDt){

    	HpCommonParModel _p = new HpCommonParModel();
    	_p.setInvsInstitutionId(invsInstitutionId);
    	_p.setDtFrom(fromDt);
    	_p.setDtTo(toDt);
		List<RealTimeMonitorModel> list = prescriptionViolateMapper.selectRealTimeMonitor(_p);
		
		List<String[]> outList =  new ArrayList<String[]>();
    	SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String[] outString = null;
		if ( list != null && list.size()>0 ) {
			for ( int i=0;i<list.size();i++ ){
				RealTimeMonitorModel record = list.get(i);
				// 每一行输出内容的数组
				outString =  new String[3];
				// 输出列计数器
				int j=0;
				// 内容
				outString[j++] = record.getTitle();
				// 医疗机构
				outString[j++] = record.getInstitutionName();
				// 时间
				outString[j++] = sdf.format(record.getViolateTm());
				
				outList.add(outString);
			}
		}
		return outList;
    }
	
	@Inject private PrescriptionViolateMapper prescriptionViolateMapper;
	@Inject private DtInvsViolationManageMapper dtInvsViolationManageMapper;
	@Inject private OutpatientService outpatientService;
}
